<html>
<head><meta charset="utf-8"><title>LLVM FFI segfault · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html">LLVM FFI segfault</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="199223013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199223013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199223013">(May 29 2020 at 23:09)</a>:</h4>
<p>Hi, I'm working on a compiler written in Rust that interfaces with LLVM's C API. I'm using the llvm-sys crate, but I'm getting a segfault when I try to get the target from the target triple. Someone in the Rust Matrix room pointed me to the Rust Zulip and I just joined, so if this is the wrong place to post this, by all means let me know!</p>



<a name="199223361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199223361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199223361">(May 29 2020 at 23:15)</a>:</h4>
<p>The type that I'm trying to pass to LLVM is <code>LLVMTarget</code> from <a href="http://docs.rs/llvm-sys">llvm-sys</a>. However, it seems to be an empty enum, which the folks at the Rust Matrix room thought meant that it couldn't be instantiated. But, the other types also seem to be empty enums, and that code works fine. Is an empty enum the way to represent an opaque FFI type?</p>



<a name="199223375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199223375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Köln <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199223375">(May 29 2020 at 23:15)</a>:</h4>
<p>what confuses me is </p>
<div class="codehilite"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">enum</span> <span class="nc">LLVMTarget</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">type</span> <span class="nc">LLVMTargetRef</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">LLVMTarget</span><span class="p">;</span><span class="w"></span>
</code></pre></div>


<p>Is that allowed?</p>



<a name="199223424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199223424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Köln <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199223424">(May 29 2020 at 23:16)</a>:</h4>
<p>looks suspiciously like the never type…</p>



<a name="199228051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199228051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199228051">(May 30 2020 at 00:27)</a>:</h4>
<p>that's allowed if you don't deref it</p>



<a name="199228057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199228057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199228057">(May 30 2020 at 00:27)</a>:</h4>
<p>that's basically how c_void works</p>



<a name="199228262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199228262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199228262">(May 30 2020 at 00:30)</a>:</h4>
<p>I figured out how to get my code working! I'm still not quite sure what the issue was though. I think I'll have to read more of the Nomicon etc</p>



<a name="199228605"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199228605" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199228605">(May 30 2020 at 00:37)</a>:</h4>
<p><span class="user-mention" data-user-id="307537">@Camelid</span> "can't be instantiated" just means you can't allocate one yourself; you could call external code and ask it for a pointer to one.</p>



<a name="199228662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199228662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199228662">(May 30 2020 at 00:38)</a>:</h4>
<p>In C, consider an opaque struct like <code>struct foo;</code>. You can't create one on the stack (not least of which because you don't know how big it is), but you can do <code>struct foo *foop = foo_new();</code>.</p>



<a name="199230659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199230659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199230659">(May 30 2020 at 01:22)</a>:</h4>
<p>Oh, I think I just realized what the issue was: I was trying to pass a pointer to a target to LLVM, but it wanted a pointer <em>to a pointer</em> to a target</p>



<a name="199232032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199232032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199232032">(May 30 2020 at 01:57)</a>:</h4>
<p>Ah, that would do it, yeah.</p>



<a name="199232044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199232044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199232044">(May 30 2020 at 01:57)</a>:</h4>
<p>Functions that create an object sometimes return a pointer, and sometimes take a double-pointer so they can fill your pointer.</p>



<a name="199232226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199232226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199232226">(May 30 2020 at 02:01)</a>:</h4>
<p>I wish I could find an in-depth tutorial for LLVM's C API, but it seems they're more focused on the C++</p>



<a name="199232413"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199232413" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199232413">(May 30 2020 at 02:05)</a>:</h4>
<p>Related question: can unsafe code cause safe code to have UB? I think the answer is yes, but I still don't fully understand how unsafe and safe interact</p>



<a name="199233568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199233568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199233568">(May 30 2020 at 02:34)</a>:</h4>
<p>Yes. As a general example unsafe code could turn an invalid raw pointer into a reference and pass that to safe code.</p>



<a name="199233643"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199233643" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199233643">(May 30 2020 at 02:37)</a>:</h4>
<p>improper unsafe code that lets safe code UB has the special name "unsound code"</p>



<a name="199235886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/LLVM%20FFI%20segfault/near/199235886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/LLVM.20FFI.20segfault.html#199235886">(May 30 2020 at 03:32)</a>:</h4>
<p>Thanks for the term! I couldn't think of how to describe it...</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>